//

// (  )
//

package com.tools.acceleratecore.xlib.util;

import android.text.TextUtils;
import java.security.Key;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class UtilEncrypt {
    public UtilEncrypt() {
    }

    public static byte[] encryptBlowFish(byte[] data, String strKey) {
        return encryptWithAlgorithm(data, strKey, "BlowFish");
    }

    public static byte[] decryptBlowFish(byte[] data, String strKey) {
        return decryptWithAlgorithm(data, strKey, "BlowFish");
    }

    public static byte[] encryptDES(byte[] data, String strKey) {
        return encryptWithAlgorithm(data, strKey, "DES");
    }

    public static byte[] decryptDES(byte[] data, String strKey) {
        return decryptWithAlgorithm(data, strKey, "DES");
    }

    public static String encryptMD5(String strData) {
        return TextUtils.isEmpty(strData) ? null : encryptMD5(strData.getBytes());
    }

    public static String encryptMD5(byte[] data) {
        if (data == null) {
            return null;
        } else {
            try {
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                md5.update(data);
                byte[] dataResult = md5.digest();
                return byteToHexString(dataResult);
            } catch (Exception var3) {
                var3.printStackTrace();
                return null;
            }
        }
    }

    public static String encryptSHA(String strData) {
        return TextUtils.isEmpty(strData) ? null : encryptMD5(strData.getBytes());
    }

    public static String encryptSHA(byte[] data) {
        if (data == null) {
            return null;
        } else {
            try {
                MessageDigest sha = MessageDigest.getInstance("SHA");
                sha.update(data);
                byte[] dataResult = sha.digest();
                return byteToHexString(dataResult);
            } catch (Exception var3) {
                var3.printStackTrace();
                return null;
            }
        }
    }

    public static String base64Encode(byte[] data) {
        return data == null ? null : Base64.encode(data);
    }

    public static byte[] base64Decode(String strData) {
        return TextUtils.isEmpty(strData) ? null : Base64.decode(strData);
    }

    private static String byteToHexString(byte[] data) {
        if (data == null) {
            return null;
        } else {
            StringBuffer sb = new StringBuffer();

            for(int nIndex = 0; nIndex < data.length; ++nIndex) {
                String strHex = Integer.toHexString(255 & data[nIndex]);
                if (strHex.length() == 1) {
                    sb.append('0');
                }

                sb.append(strHex);
            }

            return sb.toString();
        }
    }

    private static byte[] stringHexToByte(String strData) {
        if (TextUtils.isEmpty(strData)) {
            return null;
        } else if (strData.length() % 2 == 1) {
            return null;
        } else {
            byte[] dataResult = new byte[strData.length() / 2];

            for(int nIndex = 0; nIndex < strData.length(); nIndex += 2) {
                dataResult[nIndex / 2] = Integer.decode("0x" + strData.substring(nIndex, nIndex + 2)).byteValue();
            }

            return dataResult;
        }
    }

    private static byte[] encryptWithAlgorithm(byte[] data, String strKey, String strAlgorithm) {
        if (data != null && !TextUtils.isEmpty(strKey) && !TextUtils.isEmpty(strAlgorithm)) {
            try {
                Key key = getKey(strKey.getBytes(), strAlgorithm);
                Cipher cipher = Cipher.getInstance(strAlgorithm);
                cipher.init(1, key);
                return cipher.doFinal(data);
            } catch (Exception var5) {
                var5.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    private static byte[] decryptWithAlgorithm(byte[] data, String strKey, String strAlgorithm) {
        if (data != null && !TextUtils.isEmpty(strKey) && !TextUtils.isEmpty(strAlgorithm)) {
            try {
                Key key = getKey(strKey.getBytes(), strAlgorithm);
                Cipher cipher = Cipher.getInstance(strAlgorithm);
                cipher.init(2, key);
                return cipher.doFinal(data);
            } catch (Exception var5) {
                var5.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    private static Key getKey(byte[] data, String strAlgorithm) {
        if (data != null && !TextUtils.isEmpty(strAlgorithm)) {
            try {
                byte[] dataResult = new byte[8];

                for(int i = 0; i < data.length && i < dataResult.length; ++i) {
                    dataResult[i] = data[i];
                }

                Key key = new SecretKeySpec(dataResult, strAlgorithm);
                return key;
            } catch (Exception var4) {
                var4.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }
}
